package com.example.test.dialogfactory;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import easier.log.logger.Logger;
import easier.window.picker.CalendarPicker;
import easier.window.picker.CalendarPicker.ActionListener;
import easier.window.picker.CalendarPickerFactory;
import java.util.Calendar;

public class MainActivity20 extends AppCompatActivity {

    private Button mCurrent;
    private Button mFuture;
    private Button mAll;
    private CalendarPicker mDialogCurrent;
    private CalendarPicker mDialogFuture;
    private CalendarPicker mDialogMonth;
    private Button mA1970;
    private Button mType0;
    private Button mType1;
    private Button mType2;
    private Button mType3;
    private Button mTodayFrom1970;
    private Button mTodayTo2070;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity20.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main20 );
        initView();
        initViewAction();
    }

    private void initView() {

        mCurrent = (Button) findViewById( R.id.current );
        mFuture = (Button) findViewById( R.id.future );
        mAll = (Button) findViewById( R.id.all );
        mA1970 = (Button) findViewById( R.id.a1970 );
        mType0 = (Button) findViewById( R.id.type0 );
        mType1 = (Button) findViewById( R.id.type1 );
        mType2 = (Button) findViewById( R.id.type2 );
        mType3 = (Button) findViewById( R.id.type3 );
        mTodayFrom1970 = (Button) findViewById( R.id.todayFrom1970 );
        mTodayTo2070 = (Button) findViewById( R.id.todayTo2070 );
    }

    private void initViewAction() {

        mDialogCurrent = CalendarPickerFactory.buildEndTodayPicker( MainActivity20.this, new ActionListener() {
            @Override
            public void onCancelClick() {

            }

            @Override
            public void onDoneClick( Calendar calendar ) {

                Logger.d( getTime( calendar ) );
            }
        } );

        mCurrent.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mDialogCurrent.show( getSupportFragmentManager(), "current" );
            }
        } );

        mDialogFuture = CalendarPickerFactory.buildStartTodayPicker( MainActivity20.this, new ActionListener() {
            @Override
            public void onCancelClick() {

            }

            @Override
            public void onDoneClick( Calendar calendar ) {

                Logger.d( getTime( calendar ) );
            }
        } );
        mFuture.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mDialogFuture.show( getSupportFragmentManager(), "future" );
            }
        } );

        mDialogMonth = CalendarPickerFactory.buildYearMonthDayPicker( MainActivity20.this, new ActionListener() {
            @Override
            public void onCancelClick() {

            }

            @Override
            public void onDoneClick( Calendar calendar ) {

                Logger.d( getTime( calendar ) );
            }
        } );
        mAll.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mDialogMonth.show( getSupportFragmentManager(), "all" );
            }
        } );
        mA1970.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPicker picker = CalendarPickerFactory.buildFrom1970after100Year( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        CalendarPicker.setCalendarToTimeStart( calendar );
                        Logger.d( getTime( calendar ) );
                    }
                } );
                picker.show( getSupportFragmentManager(), "1970" );
            }
        } );

        mType0.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPickerFactory.buildDateTimePicker( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        Logger.d( getTime( calendar ) );
                    }
                } ).show( getSupportFragmentManager(), "mType0" );
            }
        } );

        mType1.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPickerFactory.buildDateHourMinutePicker( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        Logger.d( getTime( calendar ) );
                    }
                } ).show( getSupportFragmentManager(), "mType1" );
            }
        } );

        mType2.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPicker picker = new CalendarPicker();
                picker.setActionListener( new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        Logger.d( getTime( calendar ) );
                    }
                } );
                picker.show( getSupportFragmentManager(), "mType2" );
            }
        } );

        mType3.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPicker picker = CalendarPickerFactory.buildYearMonthDayPicker( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        Logger.d( getTime( calendar ) );
                    }
                } );
                picker.show( getSupportFragmentManager(), "mType3" );
            }
        } );

        mTodayFrom1970.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPicker picker = CalendarPickerFactory.buildFrom1970ToToday( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        Logger.d( getTime( calendar ) );
                    }
                } );
                picker.show( getSupportFragmentManager(), "mTodayFrom1970" );
            }
        } );

        mTodayTo2070.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                CalendarPicker picker = CalendarPickerFactory.buildFromTodayTo2070( v.getContext(), new ActionListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onDoneClick( Calendar calendar ) {

                        Logger.d( getTime( calendar ) );
                    }
                } );
                picker.show( getSupportFragmentManager(), "mTodayTo2070" );
            }
        } );
    }

    @SuppressLint( "DefaultLocale" )
    public static String getTime( Calendar calendar ) {

        int timeYear = calendar.get( Calendar.YEAR );
        int timeMonth = calendar.get( Calendar.MONTH ) + 1;
        int timeDay = calendar.get( Calendar.DAY_OF_MONTH );
        int hour = calendar.get( Calendar.HOUR_OF_DAY );
        int minute = calendar.get( Calendar.MINUTE );
        int second = calendar.get( Calendar.SECOND );
        int milliSecond = calendar.get( Calendar.MILLISECOND );

        return String.format( "%d-%02d-%02d %02d:%02d:%02d.%04d", timeYear, timeMonth, timeDay, hour, minute, second,
            milliSecond );
    }
}